/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sgt.onlinetest.util;

/**
 *
 * @author Administrator
 */
public class HtmlExtension {

    protected static String getSubQuestionCategoryListHTML(sgt.onlinetest.entity.QuestionCategory root, String urlFormat) {
        String html = "<ul>";
        for (sgt.onlinetest.entity.QuestionCategory c : root.getChildren()) {
            html += String.format("<li><a href='%s'>%s</a></li>", String.format(urlFormat, c.getAutoId()), c.getName());
        }
        return html;
    }

    public static String getCategoryTreeHTML(sgt.onlinetest.entity.QuestionCategory root, String urlFormat) {
        return interalGetQuestionCategoryTreeHTML(root, urlFormat, true);
    }


    public static String getNoChildCategoryTreeHTML(sgt.onlinetest.entity.QuestionCategory root, String urlFormat) {
        return interalGetQuestionCategoryTreeHTML(root, urlFormat, false);
    }

    private static String interalGetQuestionCategoryTreeHTML(sgt.onlinetest.entity.QuestionCategory root, String urlFormat, boolean createChild) {
        String tree = "";
        int i = 0;//indicate whether to highlight the node or not
        while (root != null) {
            i++;
            tree = String.format("<ul><li><a%s href='%s'>%s</a></li>%s%s</ul>",
                    i == 1 ? " class=selected" : "",
                    String.format(urlFormat, root.getAutoId()), root.getName(), tree,
                    i == 1 && createChild ? getSubQuestionCategoryListHTML(root, urlFormat) : "");
            root = root.getParent();
        }
        return tree;
    }
}
